/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.adv.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkEnterpriseAdvIntegralRule;
import com.thinkgem.jeesite.modules.hk.adv.service.HkEnterpriseAdvIntegralRuleService;

/**
 * 企业广告积分规则Controller
 * @author 罗天文
 * @version 2018-03-20
 */
@Controller
@RequestMapping(value = "${adminPath}/adv/hkEnterpriseAdvIntegralRule")
public class HkEnterpriseAdvIntegralRuleController extends BaseController {

	@Autowired
	private HkEnterpriseAdvIntegralRuleService hkEnterpriseAdvIntegralRuleService;
	
	@ModelAttribute
	public HkEnterpriseAdvIntegralRule get(@RequestParam(required=false) String id) {
		HkEnterpriseAdvIntegralRule entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkEnterpriseAdvIntegralRuleService.get(id);
		}
		if (entity == null){
			entity = new HkEnterpriseAdvIntegralRule();
		}
		return entity;
	}
	
	@RequiresPermissions("adv:hkEnterpriseAdvIntegralRule:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkEnterpriseAdvIntegralRule hkEnterpriseAdvIntegralRule, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HkEnterpriseAdvIntegralRule> page = hkEnterpriseAdvIntegralRuleService.findPage(new Page<HkEnterpriseAdvIntegralRule>(request, response), hkEnterpriseAdvIntegralRule); 
		model.addAttribute("page", page);
		return "hk/adv/hkEnterpriseAdvIntegralRuleList";
	}

	@RequiresPermissions("adv:hkEnterpriseAdvIntegralRule:view")
	@RequestMapping(value = "form")
	public String form(HkEnterpriseAdvIntegralRule hkEnterpriseAdvIntegralRule, Model model) {
		model.addAttribute("hkEnterpriseAdvIntegralRule", hkEnterpriseAdvIntegralRule);
		model.addAttribute("countrys", UserUtils.getAreaListByParentId("0"));
		if(null!=hkEnterpriseAdvIntegralRule.getProvince()){
			model.addAttribute("provinces",UserUtils.getAreaListByParentId(hkEnterpriseAdvIntegralRule.getCountry().getId()));
		}
		if(null!=hkEnterpriseAdvIntegralRule.getCity()){
			model.addAttribute("citys",UserUtils.getAreaListByParentId(hkEnterpriseAdvIntegralRule.getProvince().getId()));
		}
		return "hk/adv/hkEnterpriseAdvIntegralRuleForm";
	}

	@RequiresPermissions("adv:hkEnterpriseAdvIntegralRule:edit")
	@RequestMapping(value = "save")
	public String save(HkEnterpriseAdvIntegralRule hkEnterpriseAdvIntegralRule, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hkEnterpriseAdvIntegralRule)){
			return form(hkEnterpriseAdvIntegralRule, model);
		}
		hkEnterpriseAdvIntegralRuleService.save(hkEnterpriseAdvIntegralRule);
		addMessage(redirectAttributes, "保存企业广告积分规则成功");
		return "redirect:"+Global.getAdminPath()+"/adv/hkEnterpriseAdvIntegralRule/?enterpriseId="+hkEnterpriseAdvIntegralRule.getEnterpriseId();
	}
	
	@RequiresPermissions("adv:hkEnterpriseAdvIntegralRule:edit")
	@RequestMapping(value = "delete")
	public String delete(HkEnterpriseAdvIntegralRule hkEnterpriseAdvIntegralRule, RedirectAttributes redirectAttributes) {
		hkEnterpriseAdvIntegralRuleService.delete(hkEnterpriseAdvIntegralRule);
		addMessage(redirectAttributes, "删除企业广告积分规则成功");
		return "redirect:"+Global.getAdminPath()+"/adv/hkEnterpriseAdvIntegralRule/?repage";
	}

}